<template>
    <el-main>
        <el-row :gutter="20">
            <el-card class="box-card" style="min-height: 200px;">
                <avue-crud ref="crud" :data="configSourceListData" :option="option" :page="page" @refresh-change="refresh"
                    @size-change="sizeChange" @current-change="currentChange" @row-save="handleSave" @row-update="handleUpdate"
                    @search-change="searchChange" @search-reset="searchReset">
                    <template slot="status" slot-scope="scope">
                        <div v-if="scope.row.status === 1" style="color:#13ce66">启用</div>
                        <div v-if="scope.row.status === 0" style="color:#ff4949">禁用</div>
                    </template>
                    <template slot="is_removed" slot-scope="scope">
                        <div v-if="scope.row.is_removed === 1" style="color:#ff4949">是</div>
                        <div v-if="scope.row.is_removed === 0" style="color:#13ce66">否</div>
                    </template>
                    <template slot="is_system" slot-scope="scope">
                        <div v-if="scope.row.is_system === 1" style="color:#ff4949">是</div>
                        <div v-if="scope.row.is_system === 0" style="color:#13ce66">否</div>
                    </template>
                    <template slot-scope="scope" slot="menu">
                        <el-button :size="scope.size" :type="scope.type" v-if="scope.row.status === 1 && scope.row.is_removed === 0"
                            @click="handleDisable(scope.row)">禁用</el-button>
                        <el-button :size="scope.size" :type="scope.type" v-if="scope.row.status === 0 && scope.row.is_removed === 0"
                            @click="handleEnable(scope.row)">启用</el-button>
                        <el-button icon="el-icon-edit" :size="scope.size" :type="scope.type" v-if="scope.row.is_removed === 0" @click="$refs.crud.rowEdit(scope.row, scope.index)">编辑</el-button>
                        <el-button icon="el-icon-delete" :size="scope.size" :type="scope.type" v-if="scope.row.is_removed === 0"
                            @click="handleRemove(scope.row)">删除</el-button>
                    </template>
                    <template slot="statusForm" slot-scope="scope">
                        <div>
                            <el-radio-group v-model="scope.row.status">
                                <el-radio :label="1">启用</el-radio>
                                <el-radio :label="0">禁用</el-radio>
                            </el-radio-group>
                        </div>
                    </template>
                </avue-crud>
            </el-card>
        </el-row>
    </el-main>
</template>

<script>
    export default {
        data() {
            return {
                searchForm: {
                    keyword: '',
                    is_removed: 0
                },
                page: {
                    currentPage: 1,
                    total: 0,
                    pageSize: 10
                },
                status: "1",
                configSourceListData: [],
                permission: {},
                option: {
                    // index: true,
                    // indexLabel: '序号',
                    page: true,
                    align: 'center',
                    menuAlign: 'center',
                    size: 'small',
                    dialogWidth: '35%',
                    dialogTop: 200,
                    delBtn: false,
                    editBtn: false,
                    viewBtn: true,
                    searchBtn: true,
                    searchShow: true,
                    filterBtn: false,
                    columnBtn: false,
                    searchMenuSpan: 5,
                    column: [{
                        label: 'KEY标题',
                        prop: 'key_title',
                        span: 24,
                        rules: [{
                            required: true,
                            message: "KEY标题不能为空",
                        }]
                    }, {
                        label: '配置KEY',
                        prop: 'key_name',
                        span: 24,
                        rules: [{
                            required: true,
                            message: "配置KEY不能为空",
                        }]
                    }, {
                        label: '限制字段',
                        prop: 'key_field',
                        span: 24,
                        width: 150,
                        rules: [{
                            required: true,
                            message: "限制字段不能为空",
                        }]
                    }, {
                        label: '状态',
                        prop: 'status',
                        addDisplay: false,
                        editDisplay: true,
                        span: 24,
                        slot: true,
                        formslot: true,
                        rules: [{
                            required: true
                        }]
                    }, {
                        label: '是否删除',
                        prop: 'is_removed',
                        addDisplay: false,
                        editDisplay: false,
                        span: 24,
                        slot: true,
                        search: true,
                        type: 'select',
                        dicData: [{
                            label: '是',
                            value: 1
                        }, {
                            label: '否',
                            value: 0
                        }]
                    }, {
                        label: '系统配置源',
                        prop: 'is_system',
                        addDisplay: true,
                        editDisplay: true,
                        span: 24,
                        slot: true,
                        type: 'select',
                        dicData: [{
                            label: '是',
                            value: 1
                        }, {
                            label: '否',
                            value: 0
                        }]
                    }, {
                        label: '创建时间',
                        prop: 'created_at',
                        addDisplay: false,
                        editDisplay: false,
                        span: 24,
                        width: 140
                    }, {
                        label: '关键字',
                        prop: 'keyword',
                        addDisplay: false,
                        editDisplay: false,
                        viewDisplay: false,
                        search: true,
                        span: 24,
                        width: 180,
                        hide: true,
                        searchPlaceholder: '标题|KEY'
                    }]
                },
            }
        },
        created() {
            this.configSourceList()
        },
        methods: {
            searchChange(params, done) {
                done();
                // Object.assign(params, this.searchForm)
                this.searchForm.is_removed = params.is_removed || 0
                this.searchForm.keyword = params.keyword || ''
                this.configSourceList()
            },
            searchReset() {
                this.searchForm = {}
                this.configSourceList()
            },
            // 分页
            sizeChange(val) {
                this.page.currentPage = 1
                this.page.pageSize = val
                this.configSourceList()
            },
            // 当前页
            currentChange(val) {
                this.page.currentPage = val
                this.configSourceList()
            },
            // 刷新
            refresh() {
                this.configSourceList()
            },
            // 新增保存
            handleSave(row, done, loading) {
                this.handleAxiosFun(this.$API.ZHIHUAPI.configSourceCreate, row, done, loading)
            },
            // 修改保存
            handleUpdate(row, index, done, loading) {
                // row.status = this.status.toString()
                this.handleAxiosFun(this.$API.ZHIHUAPI.configSourceEdit, row, done, loading)
            },
            configSourceList() {
                const _this = this
                const params = {
                    keyword: _this.searchForm.keyword,
                    page: _this.page.currentPage,
                    limit: _this.page.pageSize,
                    type: _this.searchForm.is_removed,
                }
                _this.axios.post(_this.$API.ZHIHUAPI.configSourceList, _this.qs.stringify(params)).then((res) => {
                    res = res.data
                    // console.log(res)
                    if (res.code === 10000) {
                        if (res.hasOwnProperty('data') && Object.keys(res.data).length > 0) {
                            _this.configSourceListData = res.data.list
                            _this.page.total = res.data.count
                        } else {
                            _this.configSourceListData = []
                            _this.page.total = 0
                        }
                    } else {
                        _this.$message({
                            showClose: false,
                            message: res.message,
                            type: 'error'
                        })
                        _this.configSourceListData = []
                        _this.page.total = 0
                    }
                }).catch(() => {
                    _this.$message({
                        showClose: false,
                        message: '网络连接失败，请重试！',
                        type: 'error'
                    })
                    _this.configSourceListData = []
                    _this.page.total = 0
                })
            },
            // 禁用
            handleDisable(row) {
                const params = {
                    id: row.id,
                    status: 0
                }
                this.handleAxiosFun(this.$API.ZHIHUAPI.configSourceStatus, params)
            },
            // 启用
            handleEnable(row) {
                const params = {
                    id: row.id,
                    status: 1
                }
                this.handleAxiosFun(this.$API.ZHIHUAPI.configSourceStatus, params)
            },
            // 删除
            handleRemove(row) {
                const params = {
                    id: row.id
                }
                var _this = this
                _this.$confirm('是否确认删除#' + row.key_title, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function() {
                    _this.handleAxiosFun(_this.$API.ZHIHUAPI.configSourceRemove, params)
                }).catch(() => {})
            },
            handleAxiosFun(url, params, done, loading) {
                const _this = this
                _this.axios.post(url, _this.qs.stringify(params)).then((res) => {
                    if (res.data.code === 10000) {
                        _this.$message({
                            showClose: false,
                            message: res.data.message,
                            type: 'success'
                        })
                        _this.refresh()
                        if (done) {
                            done()
                        }
                    } else {
                        _this.$message({
                            showClose: false,
                            message: res.data.message,
                            type: 'error'
                        })
                        if (loading) {
                            loading()
                        }
                    }
                }).catch(() => {
                    _this.$message({
                        showClose: false,
                        message: '网络连接失败，请重试！',
                        type: 'error'
                    })
                    if (loading) {
                        loading()
                    }
                })
            }
        }
    }
</script>

<style>
</style>
